using UnityEngine;

namespace GameToolkit.BehaviourTree
{

    [System.Serializable]
    [CompositeTitle("待机")]
    [CompositeCategory]
    public class WaitUntil : ExecutableComposite
    {
        [SerializeField]
        float m_WaitSeconds;

        public override BehaviourTreeObject Create()
        {
            return new Impl();
        }

        public override string GetDescription()
        {
            return m_WaitSeconds > 0 ? $"等待{m_WaitSeconds}秒" : "保持等待";
        }

        class Impl : ActionNode
        {
            float mTime;
            WaitUntil mCfg;

            protected override void OnDestroy(BehaviourTreePlayer player)
            {
            }

            protected override void OnCreate(BehaviourTreePlayer player)
            {
                mCfg = composite as WaitUntil;
            }

            protected override void OnStart(BehaviourTreePlayer player)
            {
                mTime = 0;
            }

            protected override void OnStop(BehaviourTreePlayer player)
            {
            }

            protected override NodeState OnUpdate(BehaviourTreePlayer player)
            {
                if(mCfg.m_WaitSeconds > 0)
                {
                    mTime += player.DeltaTime;
                    return mTime >= mCfg.m_WaitSeconds ? NodeState.Success : NodeState.Running;
                }
                else
                {
                    return NodeState.Running;
                }
            }
        }
    }
}
